// pages/editdraft/editdraft.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
import api from "../../utils/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    draftID:'',
    isResubmit:'',//再次提交按钮
  },
  onfieldChange(e){
  console.log(e.detail)
  this.setData({
    dfdwmc:e.detail
  })
  },
  onfieldChangeMoney(e){
    console.log(e.detail)
    this.setData({
      money:e.detail
    })
  },
  onremarksChange(e){
    this.setData({
      remarks:e.detail
    })
  },
  addoppUnit(){
    wx.navigateTo({
      url: '/pages/addoppUnit/addoppUnit?id='+this.data.draftID,
    })
  },
//汇票确认 （状态 已确认 未确认）
  hpqueren(e){
  console.log(e,"点击确认")
    let that = this
    api.hpqueren({
      data:{
        id: e.currentTarget.dataset.id,
      },
      success:function(res){
        console.log(res.data.data, "确认 okkk")
        if (res.data.code == 200) {
          that.getDate()
        }
      }
    })
  },
 //汇票取消
 hpqixiao(e){
  console.log(e,"点击取消")
    let that = this
    api.hpquxioa({
      data:{
        id: e.currentTarget.dataset.id,
      },
      success:function(res){
        console.log(res.data.data, "取消 okkk")
        if (res.data.code == 200) {
          that.getDate()
        }
      }
    })
 },




  //保存
  save(){
    let that = this
    api.draftApplicationSave({
      method: 'post',
      data: {
        id:that.data.draftID,
        dfdwmc: that.data.dfdwmc,
        remarks: that.data.remarks,
        money:that.data.money,
      },
      success: function (res) {
        console.log(res.data.data, "点击保存/更新")
        if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          })
        }else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
    })
  },

  //提交
  submit(){
    this.save()
    setTimeout(() =>{
       if (this.data.dfdwmc =='') {
      wx.showToast({
        title: '对方单位名称不能为空',
        icon: 'none'
      })
    } else if( this.data.money == ''){
      wx.showToast({
        title: '付款金额不能为空',
        icon: 'none'
      })
    }else{
      let that = this;
      api.draftApplicationSub({
      method: 'post',
      data: {
        fkId: that.data.draftID,
        uuid: that.data.uuid,
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.showToast({
            title: '提交成功',
            icon: 'success'
          })
          wx.navigateBack({
            delta: 1,
          })
        } else {
          wx.showModal({
            content: res.data.message,
            showCancel: false,
            confirmColor: '#0041C4'
          })
        }
      }
      })
    }
    },500)
  },
  //再次提交
  resubmit() {
    this.save()
    setTimeout(() => {
      let that = this;
      api.draftApplicationResSub({
        method: 'post',
        data: {
          fkId: that.data.draftID,
          uuid: that.data.uuid,
        },
        success: function (res) {
          console.log(res.data.data, "再次提交")
          if (res.data.code == 200) {
            wx.showModal({
              content: '提交成功',
              showCancel: false,
              confirmColor: '#0041C4',
              success: function (res) {
                if (res.confirm) {
                  setTimeout(() => {
                    wx.navigateBack({
                      delta: 1,
                    })
                  }, 100);
                }
              }
            })
          } else {
            wx.showModal({
              content: res.data.message,
              showCancel: false,
              confirmColor: '#0041C4'
            })
          }
        }
      })
    }, 500);
  },
  //获取详情数据
  getDate() {
  let that = this
  api.draftgetFkDetail({
    data: {
      id: this.data.draftID
    },
    success: function (res) {
      console.log(res.data.data, "汇票 详情--编辑页面")
      if (res.data.code == 200) {
        if (res.data.data.fkDraftClaimList.length > 0) {
          for (let i = 0; i < res.data.data.fkDraftClaimList.length; i++) {
            res.data.data.fkDraftClaimList[i].money = that.addComma(res.data.data.fkDraftClaimList[i].money)
          }
          res.data.data.ycMoney = that.addComma(res.data.data.ycMoney)
          res.data.data.scMoney = that.addComma(res.data.data.scMoney)
          res.data.data.totalMoney = that.addComma(res.data.data.totalMoney)
          // res.data.data.money=  that.addComma(res.data.data.money)
        }
        that.setData({
          uuid: res.data.data.uuid,
          detailList: res.data.data,
          userDept: res.data.data.userDept,
          uuid:res.data.data.uuid,
          fkDraftClaimList:res.data.data.fkDraftClaimList,
          remarks: res.data.data.remarks,
          money: res.data.data.money,
          dfdwmc: res.data.data.dfdwmc, 
          ycMoney:res.data.data.ycMoney, 
          scMoney:res.data.data.scMoney, 
          isResubmit:res.data.data.isResubmit
          
        })
      }
    }
  })
  },
  addComma(date) {
    let str = '';
    let num = '';
    let newStr = '';
    date = date + '';
    if (date.indexOf('.') != -1) {
      num = date.split('.')[0]
      str = date.split('.')[1]
    } else {
      num = date
    }
    newStr = num.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
    newStr = str == '' ? newStr : newStr + '.' + str;
    return newStr;
  },
  //删除票据list
  delpiaoju(e) {
    console.log(e.currentTarget.dataset.id)
      let index = e.currentTarget.dataset.index;
      let that = this
      Dialog.confirm({
          message: '确定删除吗？',
        })
        .then(() => {
          api.drafdelpiaoju({
            data: {
              id: e.currentTarget.dataset.id,
            },
            success: function (res) {
              if (res.data.code == 200) {
                that.getDate()
                console.log('删除票据list')
                that.data.fkDraftClaimList.splice(index, 1)
                that.setData({
                  fkDraftClaimList: that.data.fkDraftClaimList
                })
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                })
              }
            }
          })
        })
        .catch(() => {
          // on cancel
          console.log('取消操作')
        });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      draftID: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  this.getDate()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})